﻿#include "Server.MirForms.VisualMapInfo.Control.RespawnEntry.h"
#include "../../Server.SMain.h"
#include "../Class/VisualizerGlobal.h"
#include "../../../Server/MirDatabase/MonsterInfo.h"
#include "Forms/Server.MirForms.VisualMapInfo.Control.Forms.RespawnsDetailForm.h"

using namespace Microsoft::VisualBasic::PowerPacks;
using namespace Server::MirForms::VisualMapInfo::Class;
using namespace Server::MirEnvir;
using namespace Server::MirDatabase;

namespace Server::MirForms::VisualMapInfo::Control
{

    Server::MirEnvir::Envir *RespawnEntry::getEnvir() const
    {
        return SMain::getEditEnvir();
    }

    unsigned short RespawnEntry::getRange() const
    {
        return static_cast<unsigned short>(tempRange);
    }

    void RespawnEntry::setRange(unsigned short value)
    {
        if (value > 0)
        {
            tempRange = static_cast<unsigned short>(value);
        }
        else
        {
            tempRange = 1;
        }

        RegionHighlight->Size = new Size((tempRange * 2) * VisualizerGlobal::ZoomLevel, (tempRange * 2) * VisualizerGlobal::ZoomLevel);

        RegionHighlight->Left = (X - value) * VisualizerGlobal::ZoomLevel;
        RegionHighlight->Top = (Y - value) * VisualizerGlobal::ZoomLevel;


        Details->Text = StringHelper::formatSimple("C               D            X: {0} | Y: {1} | Range: {2}", std::to_string(X), std::to_string(Y), std::to_string(getRange()));
    }

    RespawnEntry::RespawnEntry()
    {
        InitializeComponent();
        InitializeRegionHighlight();
    }

    void RespawnEntry::InitializeRegionHighlight()
    {
        RegionHighlight->Visible = false;
        RegionHighlight->BorderColor = Color::Lime;
        RegionHighlight->FillColor = Color::FromArgb(30, 200, 0, 200);

        RegionHighlight->BorderWidth = 2;
        RegionHighlight->MouseMove += new System::Windows::Forms::MouseEventHandler(this->Region_MouseMove);
        RegionHighlight->MouseDown += new System::Windows::Forms::MouseEventHandler(this->Region_MouseDown);
        RegionHighlight->MouseEnter += new System::EventHandler(this->Region_MouseEnter);
        RegionHighlight->MouseLeave += new System::EventHandler(this->Region_MouseLeave);
        RegionHighlight->MouseClick += new System::Windows::Forms::MouseEventHandler(RegionHighlight_MouseClick);
        RegionHighlight->MouseHover += RegionHighlight_MouseHover;
    }

    void RespawnEntry::Region_MouseLeave(std::any sender, EventArgs *e)
    {
        if (RegionHighlight->Visible)
        {
            this->BackColor = Color::White;
        }
        else
        {
            this->BackColor = Color::Silver;
        }

        RegionHighlight->BorderColor = Color::Lime;
        RegionHighlight->FillStyle = FillStyle::Transparent;
    }

    void RespawnEntry::Region_MouseEnter(std::any sender, EventArgs *e)
    {
        if (!RegionHighlight->Visible)
        {
            return;
        }

        this->BackColor = Color::MediumOrchid;

        RegionHighlight->BorderColor = Color::MediumOrchid;
        RegionHighlight->FillStyle = FillStyle::Solid;
        RegionHighlight->Cursor = VisualizerGlobal::Cursor;
    }

    void RespawnEntry::Region_MouseDown(std::any sender, MouseEventArgs *e)
    {
        RegionHighlight->BringToFront();

        this->BackColor = Color::Orange;
        RegionHighlight->BorderColor = Color::Orange;

        if (VisualizerGlobal::SelectedTool == VisualizerGlobal::Tool::Select)
        {
            return;
        }

        MouseDownLocation = e->Location;
    }

    void RespawnEntry::Region_MouseMove(std::any sender, MouseEventArgs *e)
    {
        if (VisualizerGlobal::SelectedTool == VisualizerGlobal::Tool::Select)
        {
            return;
        }
        if (VisualizerGlobal::SelectedTool == VisualizerGlobal::Tool::Add)
        {
            return;
        }

        if (e->Button == System::Windows::Forms::MouseButtons::Left)
        {
            if (VisualizerGlobal::SelectedTool == VisualizerGlobal::Tool::Move)
            {
                RegionHighlight->Left = e->X + RegionHighlight->Left - MouseDownLocation->X;
                RegionHighlight->Top = e->Y + RegionHighlight->Top - MouseDownLocation->Y;

                X = (RegionHighlight->Left + getRange()) / VisualizerGlobal::ZoomLevel;
                Y = (RegionHighlight->Top + getRange()) / VisualizerGlobal::ZoomLevel;

                setRange(tempRange);
            }
            else if (VisualizerGlobal::SelectedTool == VisualizerGlobal::Tool::Resize)
            {
                setRange(getRange() + static_cast<unsigned short>((MouseDownLocation->X - e->Location->X) / VisualizerGlobal::ZoomLevel));
            }
        }

        if (e->Button == System::Windows::Forms::MouseButtons::Right)
        {
            if (VisualizerGlobal::SelectedTool == VisualizerGlobal::Tool::Move)
            {
                setRange(getRange() + static_cast<unsigned short>((MouseDownLocation->X - e->Location->X) / VisualizerGlobal::ZoomLevel));
            }
            else
            {
                return;
            }
        }
    }

    void RespawnEntry::RegionHighlight_MouseClick(std::any sender, MouseEventArgs *e)
    {
        if (VisualizerGlobal::SelectedTool == VisualizerGlobal::Tool::Focus)
        {
            VisualizerGlobal::SelectedFocusType = VisualizerGlobal::FocusType::Respawn;
            VisualizerGlobal::FocusRespawnEntry = this;
            VisualizerGlobal::setFocusModeActive(true);
        }

        MonsterComboBox->Focus();
    }

    void RespawnEntry::RegionHighlight_MouseHover(std::any sender, EventArgs *e)
    {
        RegionHighlight->Cursor = VisualizerGlobal::Cursor;
    }

    void RespawnEntry::UpdateForFocus()
    {
        RegionHighlight->Left = X * VisualizerGlobal::ZoomLevel;
        RegionHighlight->Top = Y * VisualizerGlobal::ZoomLevel;
        setRange(tempRange);
    }

    void RespawnEntry::RemoveEntry()
    {
        RegionHighlight->Dispose();
        this->Dispose();
    }

    void RespawnEntry::HideControl()
    {
        RegionHighlight->Visible = false;
    }

    void RespawnEntry::ShowControl()
    {
        RegionHighlight->Visible = true;
    }

    void RespawnEntry::HideRegion()
    {
        RegionHighlight->Visible = false;
        this->BackColor = Color::Silver;
        RegionHidden = true;
    }

    void RespawnEntry::ShowRegion()
    {
        RegionHighlight->Visible = true;
        this->BackColor = Color::White;
        RegionHidden = false;
    }

    void RespawnEntry::RespawnEntry_Load(std::any sender, EventArgs *e)
    {

        for (int i = 0; i < getEnvir()->MonsterInfoList.size(); i++)
        {
            MonsterComboBox->Items->Add(getEnvir()->MonsterInfoList[i]);
        }

        MonsterComboBox->SelectedIndex = MonsterIndex - 1; //-1

        Details->Text = StringHelper::formatSimple("C               D            X: {0} | Y: {1} | Range: {2}", std::to_string(X), std::to_string(Y), std::to_string(getRange()));
    }

    void RespawnEntry::MonsterComboBox_SelectedIndexChanged(std::any sender, EventArgs *e)
    {
        MonsterInfo *info = dynamic_cast<MonsterInfo*>(MonsterComboBox->SelectedItem);
        if (info == nullptr)
        {
            return;
        }
        MonsterIndex = info->Index;
    }

    void RespawnEntry::Details_DoubleClick(std::any sender, EventArgs *e)
    {
        Forms::RespawnsDetailForm *RespawnDetail = new Forms::RespawnsDetailForm();

        RespawnDetail->X->Text = std::to_string(X);
        RespawnDetail->Y->Text = std::to_string(Y);
        RespawnDetail->Spread->Text = std::to_string(getRange());
        RespawnDetail->Count->Text = Count->Text;
        RespawnDetail->Delay->Text = Delay->Text;
        RespawnDetail->RoutePath->Text = RoutePath;
        RespawnDetail->Direction->Text = std::to_string(Direction);
        RespawnDetail->RDelay->Text = std::to_string(RandomDelay);

        RespawnDetail->ShowDialog();

        X = StringHelper::fromString<short>(RespawnDetail->X->Text);
        Y = StringHelper::fromString<short>(RespawnDetail->Y->Text);
        setRange(StringHelper::fromString<unsigned short>(RespawnDetail->Spread->Text));
        Count->Text = RespawnDetail->Count->Text;
        Delay->Text = RespawnDetail->Delay->Text;
        RoutePath = RespawnDetail->RoutePath->Text;
        Direction = static_cast<unsigned char>(RespawnDetail->Direction->Text);
        RandomDelay = static_cast<unsigned short>(RespawnDetail->RDelay->Text);

        RespawnDetail->Dispose();

        delete RespawnDetail;
    }

    void RespawnEntry::Dispose(bool disposing)
    {
        if (disposing && (components != nullptr))
        {
            components->Dispose();
        }
        UserControl::Dispose(disposing);
    }

    void RespawnEntry::InitializeComponent()
    {
        this->MonsterComboBox = new System::Windows::Forms::ComboBox();
        this->Details = new System::Windows::Forms::Label();
        this->Selected = new System::Windows::Forms::CheckBox();
        this->Count = new System::Windows::Forms::TextBox();
        this->Delay = new System::Windows::Forms::TextBox();
        this->SuspendLayout();
        // 
        // MonsterComboBox
        // 
        this->MonsterComboBox->Anchor = (static_cast<System::Windows::Forms::AnchorStyles*>(((System::Windows::Forms::AnchorStyles::Top | System::Windows::Forms::AnchorStyles::Left) | System::Windows::Forms::AnchorStyles::Right)));
        this->MonsterComboBox->DropDownStyle = System::Windows::Forms::ComboBoxStyle::DropDownList;
        this->MonsterComboBox->FormattingEnabled = true;
        this->MonsterComboBox->Location = new System::Drawing::Point(24, 3);
        this->MonsterComboBox->Name = "MonsterComboBox";
        this->MonsterComboBox->Size = new System::Drawing::Size(222, 21);
        this->MonsterComboBox->TabIndex = 2;
        this->MonsterComboBox->SelectedIndexChanged += new System::EventHandler(this->MonsterComboBox_SelectedIndexChanged);
        this->MonsterComboBox->MouseEnter += new System::EventHandler(this->Region_MouseEnter);
        this->MonsterComboBox->MouseLeave += new System::EventHandler(this->Region_MouseLeave);
        // 
        // Details
        // 
        this->Details->AutoSize = true;
        this->Details->Location = new System::Drawing::Point(3, 30);
        this->Details->Name = "Details";
        this->Details->Size = new System::Drawing::Size(103, 13);
        this->Details->TabIndex = 3;
        this->Details->Text = "C               D            ";
        this->Details->DoubleClick += new System::EventHandler(this->Details_DoubleClick);
        this->Details->MouseEnter += new System::EventHandler(this->Region_MouseEnter);
        this->Details->MouseLeave += new System::EventHandler(this->Region_MouseLeave);
        // 
        // Selected
        // 
        this->Selected->AutoSize = true;
        this->Selected->Location = new System::Drawing::Point(3, 7);
        this->Selected->Name = "Selected";
        this->Selected->Size = new System::Drawing::Size(15, 14);
        this->Selected->TabIndex = 4;
        this->Selected->UseVisualStyleBackColor = true;
        this->Selected->MouseEnter += new System::EventHandler(this->Region_MouseEnter);
        this->Selected->MouseLeave += new System::EventHandler(this->Region_MouseLeave);
        // 
        // Count
        // 
        this->Count->Location = new System::Drawing::Point(19, 27);
        this->Count->Name = "Count";
        this->Count->Size = new System::Drawing::Size(28, 20);
        this->Count->TabIndex = 5;
        this->Count->Text = "0";
        this->Count->MouseEnter += new System::EventHandler(this->Region_MouseEnter);
        this->Count->MouseLeave += new System::EventHandler(this->Region_MouseLeave);
        // 
        // Delay
        // 
        this->Delay->Location = new System::Drawing::Point(70, 27);
        this->Delay->Name = "Delay";
        this->Delay->Size = new System::Drawing::Size(28, 20);
        this->Delay->TabIndex = 6;
        this->Delay->Text = "0";
        this->Delay->MouseEnter += new System::EventHandler(this->Region_MouseEnter);
        this->Delay->MouseLeave += new System::EventHandler(this->Region_MouseLeave);
        // 
        // RespawnEntry
        // 
        this->AutoScaleDimensions = new System::Drawing::SizeF(6.0F, 13.0F);
        this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
        this->BackColor = System::Drawing::Color::White;
        this->Controls->Add(this->Delay);
        this->Controls->Add(this->Count);
        this->Controls->Add(this->Selected);
        this->Controls->Add(this->Details);
        this->Controls->Add(this->MonsterComboBox);
        this->Name = "RespawnEntry";
        this->Size = new System::Drawing::Size(249, 50);
        this->Load += new System::EventHandler(this->RespawnEntry_Load);
        this->MouseEnter += new System::EventHandler(this->Region_MouseEnter);
        this->MouseLeave += new System::EventHandler(this->Region_MouseLeave);
        this->ResumeLayout(false);
        this->PerformLayout();

    }
}
